﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MarkDownFileSplitter
{
    internal class TreeNodeStateInfo
    {
        public Dictionary<string, TreeNodeStateInfoItem>  Items=new Dictionary<string, TreeNodeStateInfoItem>();

        public void Save(TreeView treeView)
        {
            for(var i = 0; i < treeView.Nodes.Count; i++)
            {
                Items.Add(treeView.Nodes[i].Text, new TreeNodeStateInfoItem()
                {
                    IsExpand = treeView.Nodes[i].IsExpanded
                });
                Save(treeView.Nodes[i]);
            }
        }
        private void Save(TreeNode tn)
        {
            for (var i = 0; i < tn.Nodes.Count; i++)
            {
                Items.Add(tn.Nodes[i].FullPath, new TreeNodeStateInfoItem()
                {
                    IsExpand = tn.Nodes[i].IsExpanded
                });
                Save(tn.Nodes[i]);
            }
        }
        public void Restore(TreeView treeView)
        {
            for (var i = 0; i < treeView.Nodes.Count; i++)
            {
                var tn= treeView.Nodes[i];
                if (Items.ContainsKey(tn.Text))
                {
                    var t = Items[tn.Text];
                    t.Restore(tn);
                }
                Restore(tn);
            }
        }
        private void Restore(TreeNode tn)
        {
            for (var i = 0; i < tn.Nodes.Count; i++)
            {
                var tn2 = tn.Nodes[i];
                if (Items.ContainsKey(tn2.Text))
                {
                    var t = Items[tn2.Text];
                    t.Restore(tn2);
                }
                Restore(tn2);
            }
        }


    }

    class TreeNodeStateInfoItem
    {
        public bool IsExpand { get; set;}

        internal void Restore(TreeNode tn2)
        {
            if (IsExpand && tn2.IsExpanded == false)
            {
                tn2.Expand();
            }
            if (IsExpand == false && tn2.IsExpanded)
            {
                tn2.Collapse();
            }
        }
    }
}
